package org.darkforest.controller;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.darkforest.auth.AuthPassport;
import org.darkforest.auth.CurrentMember;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.IdeasFeedbackMapper;
import org.darkforest.model.IdeasFeedback;
import org.darkforest.model.Invoice;
import org.darkforest.model.Member;
import org.darkforest.model.RestEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "ideasFeedback")
@Api(value = "ideasFeedback", description = "意见与反馈",produces= MediaType.APPLICATION_JSON_VALUE)
public class IdeasFeedbackController {

    @Autowired
    private IdeasFeedbackMapper deasFeedbackMapper;

    @AuthPassport
    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ApiOperation(value = "获取意见与反馈列表", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity getIdeasFeedbacklist(@CurrentMember Member member){
        try{
            IdeasFeedback ideasFeedback = new IdeasFeedback();
            ideasFeedback.setCreateMemberId(member.getMemberId());
            return RestEntity.success(deasFeedbackMapper.selectByCondition(ideasFeedback));
        }catch (Exception e){
            return RestEntity.failed();
        }
    }

    /**
     * ajax添加，新增成功时返回包含主键的对象
     * @param ideasFeedback
     * @return
     */
    @AuthPassport
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ApiOperation(value = "新增意见与反馈", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity save(@CurrentMember Member member, @RequestBody IdeasFeedback ideasFeedback){
        try{

            ideasFeedback.setCreateMemberId(member.getMemberId());
            ideasFeedback.setCreateDatetime(DateUtil.unixTimeStampInt().intValue());

            deasFeedbackMapper.insertSelective(ideasFeedback);

            return RestEntity.success(ideasFeedback);
        } catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }
}
